<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="600px"
    top="5%"
  >
    <t-form ref="form" :colon="true" :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="字典类型" name="dictType">
        <t-input clearable v-model="formData.dictType" :disabled="true" placeholder="请输入字典类型"></t-input>
      </t-form-item>
      <t-form-item label="数据标签" name="dictLabel">
        <t-input clearable v-model="formData.dictLabel"  placeholder="请输入数据标签"></t-input>
      </t-form-item>
      <t-form-item label="数据键值" name="dictValue">
        <t-input clearable v-model="formData.dictValue"  placeholder="请输入数据键值"></t-input>
      </t-form-item>
      <t-form-item label="样式属性" name="cssClass">
        <t-input clearable v-model="formData.cssClass"  placeholder="请输入样式属性"></t-input>
      </t-form-item>
      <t-form-item label="显示排序" name="dictSort">
      <t-input-number clearable v-model="formData.dictSort" :min="0" placeholder="请输入"></t-input-number>
      </t-form-item>
      <t-form-item label="回显样式" name="listClass">
        <t-select v-model="formData.listClass">
          <t-option
            v-for="item in listClassOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></t-option>
        </t-select>
      </t-form-item>
      <t-form-item label="状态" name="status">
        <t-radio-group :options="dict.type.sys_normal_disable" v-model="formData.status">
        </t-radio-group>
      </t-form-item>
      <t-form-item label="备注" name="remark">
        <t-textarea
          v-model="formData.remark"
          placeholder="请输入内容"
        />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";


export default {
  name: "add-edit-dict-data",
  dicts: ['sys_normal_disable'],
  props:["header","dictCode", "dictType"],
  data() {
    return {
      visible: true,
      loading: false,
      listClassOptions: [
        {
          value: "default",
          label: "默认"
        },
        {
          value: "primary",
          label: "主要"
        },
        {
          value: "success",
          label: "成功"
        },
        // {
        //   value: "info",
        //   label: "信息"
        // },
        {
          value: "warning",
          label: "警告"
        },
        {
          value: "danger",
          label: "危险"
        }
      ],
      formData: {
        dictCode: undefined,
        dictLabel: '',
        dictValue: '',
        cssClass: "",
        listClass: 'default',
        dictSort: 0,
        status: "0",
        remark: "",
        dictType: this.dictType,
      },
      rules: {
        dictLabel: [
          { required: true, message: "数据标签不能为空" }
        ],
        dictValue: [
          { required: true, message: "数据键值不能为空" }
        ],
        dictSort: [
          { required: true, message: "数据顺序不能为空" }
        ]
      }
    }
  },
  watch: {
    dictCode: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods:{
    getObj(){
      getData(this.dictCode).then((response) => {
        this.formData = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.dictCode !== 0) {
            updateData(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            addData(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    handleDelete(dictCode){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`是否确认删除字典编码为"${dictCode}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          delData(dictCode).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    }
  }
}
</script>

<style scoped>

</style>
